<?php
namespace app\app\controller;

use alisms\Alidayu;
use app\common\controller\App;
use Common\utils\Alisms;
use extend\alisms\AlismsApi;
use think\captcha\Captcha;
use think\Config;
use think\console\command\optimize\Schema;
use think\console\command\Version;
use think\Db;
use think\Exception;
use think\route\Rule;

/**
 * 公共方法，发送短信、省市区三级联动等
 */
class Common extends App
{
    /**
     * 发送短信验证码
     * @return [type] [description]
     */
    public function sendcode()
    {

        $mobile = input('mobile');
        if (empty($mobile)) {
            return jsonResult('请输入手机号码', 0);
        }
        $code = sprintf("%06s", mt_rand('000000', '999999'));
        $alidayu = new Alidayu();
        $resp = $alidayu->sendSms($mobile, $code);
        model("MobileCode")->save([
            'mobile' => $mobile,
            'code' => $code
        ]);
        if ($resp->Code == 'OK') {
            return jsonResult('发送成功');
        } else {
            return jsonResult($resp->Message, 0);
        }
    }

    /*
     * 图片验证码
     * */
    public function verify()
    {
        $config =    [
            'fontSize'    =>  50,
            'length'      =>  4,
            'useNoise'   =>false,
            'useCurve'   =>false
        ];
        $captcha=new Captcha($config);
        return $captcha->entry();
    }


    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');

        if (empty($file)) {
            return $this->fail('请上传文件');
        }
        $rootPath = './'; // 设置附件上传根目录
        $subName  = 'uploads';
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->validate(['size' => 614572800, 'ext' => 'jpg,png,gif,jpeg'])->move(Config('account.upload_url'). $subName);

        if ($info) {
            $file_info = $info->getinfo();
            $save_path = str_replace('\\', '/', $info->getSaveName());
            //添加到file表
            $info->md5();
            $path = ltrim($rootPath . $subName . '/' . $save_path, '.');
            $new_path=substr($save_path,0, stripos($save_path,"/"));



//            $qianshui_path=$rootPath.$subName.'/'.$new_path;
//            if (!is_dir('../../qianshui/'.$qianshui_path)){
//                mkdir('../../qianshui/'.$qianshui_path,0700);
//            }
//            copy(request()->domain() . $path,'../../qianshui/'.$path);



            $data = [
                'title'     => $file_info['name'],
                'md5'       => $info->md5(),
                'path'      => $path,
                'size'      => $file_info['size'],
                'create_time'   => time(),
            ];

            $id = Db::name('File')->insertGetId($data);
            if ($id) {
                $data=[
                    'id'=>$id,
                    'path'=>getHost(). $path,
                ];
                return $this->ok('上传成功',$data);
            } else {

                return $this->fail('上传失败');
            }
        } else {
            // 上传失败获取错误信息
            return $this->fail($file->getError());
        }
    }


    public function send_code(){
        $mobile =request()->param('mobile');
        $send_type=request()->param("send_type",1);
        if (empty($mobile)){
            return $this->fail('请输入您的手机号');
        }

        if (!checkmobile($mobile)){
            return $this->fail('手机号码格式有误');
        }

        $code = mt_rand('0000','9999');
        $mobile_data = array(
            'mobile'=>$mobile,
            'verify_code'=>$code,
            'addtime'=>time(),
            'beizhu'=>'pc注册'
        );
        $alidayu = new Alidayu();
        $resp = $alidayu->sendSms($mobile,$code,$send_type);

        if ($resp->Code == 'OK') {
            if ($send_type==1){
                Db::name("Mobilelog")->insert($mobile_data);
            }

            return $this->ok('发送成功');
        } else {
            return $this->fail('发送失败');
        }


    }


    //获取省市区
    public function get_area()
    {

        $id=request()->param('id');
        if (empty($id)){
            $where[]=['parent_id','=',1];
        }else{
            $where[]=['parent_id','=',$id];
        }
        $area=Db::name("Area")->where($where)->field('id,title')->select();

        return $this->ok('成功',$area);
    }



    /*
     * 用户协议
     * */
    public function xieyi()
    {

        $xieyi=model("Xieyi")->order("id desc")->find();

       return $this->ok('成功',$xieyi);

    }



}
